package hc.j2me.ui;

import hc.core.ContextManager;
import hc.core.IConstant;
import hc.core.MsgBuilder;
import hc.j2me.ScreenClientManager;
import hc.j2me.Starter;
import hc.j2me.util.ImageUtility;
import hc.j2me.util.SoundUtil;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

public class ConfigForm extends Form implements CommandListener{
	ChoiceGroup cgColorMode, cgMute, cgAlpha, cgEnglish, cgOnlyTCP;
	TextField refreshMS;
	
	int[] vC = {IConstant.COLOR_64_BIT, IConstant.COLOR_32_BIT, 
			IConstant.COLOR_16_BIT, IConstant.COLOR_8_BIT, IConstant.COLOR_4_BIT};

	public ConfigForm(String title) {
		super(title);
		final String stable = Starter.getUIString("m13");
		String[] colorMode = {"☆☆☆☆☆", "☆☆☆☆/★" + stable, "☆☆☆/★★" + stable, "☆☆/★★★" + stable, "☆/★★★★" + stable};
		cgColorMode = new ChoiceGroup(Starter.getUIString("m03"), Choice.EXCLUSIVE,
				colorMode, null);
		append(cgColorMode);

		String[] muteStrings = { Starter.getUIString("m04") };
		cgMute = new ChoiceGroup("", Choice.MULTIPLE, muteStrings, null);
		if(SoundUtil.isSupportTone()){
			append(cgMute);
		}
		
		String[] alphaStrings = { Starter.getUIString("m11") };
		cgAlpha = new ChoiceGroup("", Choice.MULTIPLE, alphaStrings, null);
		if(ImageUtility.isHardAlpha){
			append(cgAlpha);
		}
		
		refreshMS = new TextField(Starter.getUIString("m05"),
				String.valueOf(Starter.config.refreshMS),14,TextField.NUMERIC);
		append(refreshMS);
		
		String[] strOnlyTCP = { Starter.getUIString("m14") };
		cgOnlyTCP = new ChoiceGroup("", Choice.MULTIPLE, strOnlyTCP, null);
		append(cgOnlyTCP);

		String[] english = { "English (need restart me)" };
		cgEnglish = new ChoiceGroup("", Choice.MULTIPLE, english, null);
		append(cgEnglish);

		int index = 0;//colorMode.length;
		int ColorBit = Starter.config.colorBit; 
		for (int i = 0; i < colorMode.length; i++) {
			if(ColorBit == vC[i]){
				index = i;
				break;
			}
		}
		cgColorMode.setSelectedIndex(index, true);
		
		cgMute.setSelectedIndex(0, Starter.config.isMute);
		cgAlpha.setSelectedIndex(0, Starter.config.isUseAlpha);
		cgOnlyTCP.setSelectedIndex(0, Starter.config.isTCPOnly);
		cgEnglish.setSelectedIndex(0, Starter.config.userLang.equals(RSConfig.EN_US));
		
		addCommand(Starter.cmd_ok);
		addCommand(Starter.cmd_cancel);
		setCommandListener(this);
	}

	private final byte[] colorModeTwoBytes = new byte[2 + MsgBuilder.INDEX_MSG_DATA];
	
	public void commandAction(Command c, Displayable d) {
		if(c == Starter.cmd_ok){
			int msSecond = Integer.parseInt(refreshMS.getString());
			if(msSecond < 100){
				msSecond = 100;
			}else if(msSecond > 10000){
				msSecond = 10000;
			}
			Starter.config.refreshMS = msSecond;
			
			int i = cgColorMode.getSelectedIndex();
			Starter.config.colorBit = vC[i];
			
			boolean[] rs = {false};

			if(cgMute != null && SoundUtil.isSupportTone()){
				cgMute.getSelectedFlags(rs);
				Starter.config.isMute = rs[0];
			}
			
			cgAlpha.getSelectedFlags(rs);
			Starter.config.isUseAlpha = rs[0];
			
			cgOnlyTCP.getSelectedFlags(rs);
			Starter.config.isTCPOnly = rs[0];
			
			cgEnglish.getSelectedFlags(rs);
			Starter.config.userLang = (rs[0]?RSConfig.EN_US:RSConfig.getSysLocale());
			
			//存储
			Starter.update(Starter.config);
			
			if(ContextManager.cmStatus == ContextManager.STATUS_CLIENT_SELF){
				ScreenClientManager.setRefreshMillSecond(Starter.config.refreshMS);
				ScreenClientManager.setColorMode(colorModeTwoBytes, Starter.config.colorBit);
			}
			
		}else if(c == Starter.cmd_cancel){
			
		}
		UIManager.exitOrReturn();
	}

	public static void showConfigForm() {
		UIManager.pushInAndSwithToNew(new ConfigForm(Starter.getUIString("m06")));
	}
}
